﻿using AbpToDomain.EntityInfo;
using AbpToDomain.View;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;
using Volo.Abp.Data;
using Volo.Abp.EntityFrameworkCore;

namespace AbpEntityFrameworkcore.DbContext
{
    [ConnectionStringName("Lmsconnstr")]
    public class MyDbContext : AbpDbContext<MyDbContext>
    {

        public DbSet<Box> boxs { get; set; }
    
        public DbSet<MaterialBase> materialBases { get; set; }
        public DbSet<MaterialOrder> materialOrders { get; set; }
        public DbSet<Warehouse> warehouses { get; set; }
        public DbSet<BoxGrid> boxGrids { get; set; }
        public DbSet<BoxPosition> boxPositions { get; set; }
        public DbSet<WarehouseLine> warehouseLines { get; set; }
        public DbSet<WorkerOrder> workerOrders { get; set; }

        public DbSet<View_Materials> viewMaterials { get; set; }
        public DbSet<View_User> viewUser { get; set; }
       


      
        public MyDbContext(DbContextOptions<MyDbContext> options) : base(options)
        {

      
        }
     

     

        protected override void OnModelCreating(ModelBuilder builder)
        {
            base.OnModelCreating(builder);
            builder.Entity<Warehouse>(n =>
            {
                n.ToTable("Warehouse");       
            });
            builder.Entity<Box>(n =>
            {
                n.ToTable("Box");              
            });
            builder.Entity<MaterialBase>(n =>
            {
                n.ToTable("MaterialBase");             
            });
            builder.Entity<MaterialOrder>(n =>
            {
                n.ToTable("MaterialOrder");
             
                n.HasMany<MaterialBase>(s => s.materialBases).WithMany(t => t.materialOrders).
                UsingEntity(j => j.ToTable("T_MaterialBases_MaterialOrders"));
            });
            builder.Entity<BoxGrid>(n =>
            {
                n.ToTable("BoxGrid");
                n.HasOne<Box>(c=>c.TheBox).WithMany(a=>a.BoxGrids).IsRequired();

            });
            builder.Entity<BoxPosition>(n =>
            {
                n.ToTable("BoxPosition");
                n.HasOne<BoxGrid>(c=>c.TheBoxGrid).WithMany(a=>a.BoxPositions).IsRequired();
                n.HasOne<MaterialBase>(o => o.MaterialBase).WithOne(d => d.BoxPosition).
                   HasForeignKey<MaterialBase>(o => o.Id_BoxPosition);
            });
            builder.Entity<WarehouseLine>(n =>
            {
                n.ToTable("WarehouseLine");
                n.HasOne<Warehouse>(c => c.TheWarehouse).WithMany(a => a.warehouselines).IsRequired();
                n.HasOne<Box>(o => o.Box).WithOne(d => d.WarehouseLine).HasForeignKey<Box>(o => o.Id_WarehouseLine);
            });
            builder.Entity<WorkerOrder>(n =>
            {
                n.ToTable("WorkerOrder");
                n.HasOne<MaterialOrder>(c=>c.TheMaterialOreder).WithMany(a=>a.workerOrders).IsRequired();
            });



            //view
            builder.Entity<View_Materials>(n =>
            {
                n.ToTable("View_Materials").HasNoKey();
            });
            builder.Entity<View_User>(n =>
            {
                n.ToTable("View_User").HasNoKey();
            });
        }


    
    }
  

}
